<template>
  <div class="project-settings-view">
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <h2>项目设置</h2>
          <p class="description">配置项目的部署和服务器设置</p>
        </div>
        <router-link :to="`/project/${projectId}`" class="back-btn">
          <i class="fas fa-arrow-left"></i>
          返回项目
        </router-link>
      </div>
    </div>

    <div class="main-content" v-loading="loading">
      <project-settings 
        v-if="project"
        :project="project"
        @save="handleSettingsSave"
        @cancel="handleCancel"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import ProjectSettings from '../../components/ProjectSettings.vue'
import { projectApi } from '../../api/project'

const route = useRoute()
const router = useRouter()
const projectId = route.params.id
const project = ref(null)
const loading = ref(false)

// 获取项目信���
const fetchProject = async () => {
  try {
    loading.value = true
    const data = await projectApi.getProjectById(projectId)
    project.value = data
  } catch (error) {
    ElMessage.error('获取项目信息失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

// 保存设置
const handleSettingsSave = async (settings) => {
  try {
    loading.value = true
    await projectApi.updateProject(projectId, settings)
    ElMessage.success('设置已保存')
    router.push(`/project/${projectId}`)
  } catch (error) {
    ElMessage.error('保存失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

// 取消
const handleCancel = () => {
  router.push(`/project/${projectId}`)
}

onMounted(() => {
  fetchProject()
})
</script>

<style scoped>
.project-settings-view {
  min-height: 100vh;
  background: var(--background-primary);
}

.page-header {
  background: var(--background-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: var(--spacing-lg) var(--spacing-xl);
  margin-bottom: var(--spacing-xl);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left h2 {
  margin-bottom: var(--spacing-xs);
}

.description {
  color: var(--text-secondary);
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-lg);
  background: var(--background-tertiary);
  color: var(--text-primary);
  border-radius: var(--radius-lg);
  text-decoration: none;
  transition: all var(--transition-fast);
}

.back-btn:hover {
  background: var(--background-quaternary);
  transform: translateX(-2px);
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--spacing-xl);
}
</style> 